package com.Cost.controller;

import com.Cost.entity.Cost;
import com.Cost.service.CostService;
import com.Cost.utils.ResponseResult;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 收费信息 前后端交互类，仅保留Cost单表增删改查
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/cost")
public class CostController {

    @Autowired
    private CostService costService;

    /**
     * 添加收费信息（单表插入，仅操作Cost表字段）
     */
    @RequestMapping(value = "/add.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackAddCostInfo")
    public ResponseResult addCostInfo(@RequestBody Cost cost, HttpServletRequest request) throws IOException {
        // 直接调用添加方法，仅操作Cost单表
        if (costService.doAddCostInfo(cost)) {
            return ResponseResult.build(true, "添加收费信息成功！");
        } else {
            return ResponseResult.build(false, "添加收费信息失败，请稍后重试！");
        }
    }

    /**
     * 添加收费信息失败时的降级方法
     */
    public ResponseResult fallbackAddCostInfo(@RequestBody Cost cost, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 添加收费信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 修改收费信息（单表更新，仅依据Cost表字段修改）
     */
    @RequestMapping(value = "/modify.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackModifyCostInfo")
    public ResponseResult modifyCostInfo(@RequestBody Cost cost, HttpServletRequest request) throws IOException {
        if (costService.doModifyCostInfo(cost)) {
            return ResponseResult.build(true, "修改收费信息成功！");
        } else {
            return ResponseResult.build(false, "修改收费信息失败，可能是当前收费信息不存在！");
        }
    }

    /**
     * 修改收费信息失败时的降级方法
     */
    public ResponseResult fallbackModifyCostInfo(@RequestBody Cost cost, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 修改收费信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 删除收费信息（单表删除，依据Cost表主键等标识删除）
     */
    @RequestMapping(value = "/delete.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackDeleteCostInfo")
    public ResponseResult deleteCostInfo(@RequestBody Cost cost, HttpServletRequest request) throws IOException {
        if (costService.doDeleteCostInfo(cost)) {
            return ResponseResult.build(true, "删除收费信息成功！");
        } else {
            return ResponseResult.build(false, "删除收费信息失败，可能是当前收费信息不存在！");
        }
    }

    /**
     * 删除收费信息失败时的降级方法
     */
    public ResponseResult fallbackDeleteCostInfo(@RequestBody Cost cost, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 删除收费信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 获取单个收费信息（单表查询，仅查询Cost表数据）
     */
    @RequestMapping(value = "/get.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackGetCostInfo")
    public ResponseResult getCostInfo(@RequestBody Cost cost, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Cost pCost = costService.doGetCostInfo(cost);
        return ResponseResult.build(pCost != null, null, pCost);
    }

    /**
     * 获取单个收费信息失败时的降级方法
     */
    public ResponseResult fallbackGetCostInfo(@RequestBody Cost cost, HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 获取收费信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，返回临时数据", new Cost());
    }

    /**
     * 获取收费列表（单表查询，查询Cost表全部数据）
     */
    @RequestMapping(value = "/list.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackGetCostInfoList")
    public ResponseResult getCostInfoList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Cost> list = costService.doGetCostInfoList();
        return ResponseResult.build(list != null, null, list);
    }

    /**
     * 获取收费列表失败时的降级方法
     */
    public ResponseResult fallbackGetCostInfoList(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 获取收费列表失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，返回部分数据", Collections.emptyList());
    }

    /**
     * 查询收费列表（单表条件查询，依据Cost表字段筛选）
     */
    @RequestMapping(value = "/query.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "costService", fallbackMethod = "fallbackQueryCostInfoList")
    public ResponseResult queryCostInfoList(@RequestBody Cost cost, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Cost> list = costService.doQueryCostInfoList(cost);
        return ResponseResult.build(list != null, null, list);
    }

    /**
     * 查询收费列表失败时的降级方法
     */
    public ResponseResult fallbackQueryCostInfoList(@RequestBody Cost cost, HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 查询收费列表失败: " + throwable.getMessage());
        return ResponseResult.success("查询部分数据", Collections.emptyList());
    }
}